package com.rui.study.algorithm.L_深入理解JVM_方法调用;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.lookup;

public class DispatchByUser {
    class Grandpa {
        void thinking () {
            System.out.println("Grandpa is thinking");
        }
    }
    class Father extends Grandpa {
        @Override
        void thinking () {
            System.out.println("Father is thinking");
        }
    }
    class Other extends Father {
        @Override
        void thinking () {
            System.out.println("Other is thinking");
        }
    }
    class Son extends Other {
        @Override
        void thinking () {
            MethodType mt = MethodType.methodType(void.class);
            try {
                MethodHandle mh = lookup().findSpecial(Grandpa.class, "thinking", mt, this.getClass());
                mh.invoke(this);
            } catch (Throwable throwable) {

            }
        }
    }

    public static void main(String[] args) {
        DispatchByUser dispatch = new DispatchByUser();
        dispatch.new Son().thinking();
    }
}
